package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.dao.SpuImgMapper;
import com.mall.dao.SpuInfoMapper;
import com.mall.entity.SpuImages;
import com.mall.entity.SpuInfo;
import com.mall.service.SearchSpu;
import com.mall.vo.ErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Service
public class SearchSpulmpl implements SearchSpu {
    @Autowired
    SpuInfoMapper spuInfoMapper;
    @Autowired
    SpuImgMapper spuImgMapper;
    @Override
    public List<SpuInfo> searchByCat(Integer parentid, Integer start , Integer pagesize, String sort) {
        String con=null;
        String sor = null;
        start=(start-1)*pagesize;
        List<SpuInfo> spuInfos =null;
        if (!StringUtils.isEmpty(sort)&&sort!=" "){
            String[] s = sort.split("-");
            if (s[0].equals("update_time")||s[0].equals("min_price")||s[0].equals("sale_count")){
                con = s[0];
            }
            if (s[1].equals("DESC")||s[1].equals("ASC")){
                sor = s[1];
            }
        }
        try {
             spuInfos = spuInfoMapper.searchByCatParentId(parentid, start, pagesize,con,sor);
        }catch (Exception e){
            e.printStackTrace();
        }
        QueryWrapper<SpuImages> queryWrapper = new QueryWrapper<>();
        spuInfos.forEach(spuInfo -> {
            queryWrapper.eq("spu_id",spuInfo.getId());
            spuInfo.setImages(spuImgMapper.selectList(queryWrapper));
            queryWrapper.clear();
        });
        return spuInfos;
    }

    @Override
    public List<SpuInfo> searchByKeyWord(String keyword, Integer start, Integer pagesize,String sort) {
        start=(start-1)*pagesize;
        String con=null;
        String sor = null;
        List<SpuInfo> spuInfos =null;
        if (!StringUtils.isEmpty(sort)&&sort!=" "){
            String[] s = sort.split("-");
            if (s[0].equals("update_time")||s[0].equals("min_price")||s[0].equals("sale_count")){
                con = s[0];
            }
            if (s[1].equals("DESC")||s[1].equals("ASC")){
                sor = s[1];
            }
        }
        try {
            spuInfos = spuInfoMapper.searchByKeyWord(keyword, start, pagesize,con,sor);
        }catch (Exception e){
            e.printStackTrace();
        }
        QueryWrapper<SpuImages> queryWrapper = new QueryWrapper<>();
        spuInfos.forEach(spuInfo -> {
            queryWrapper.eq("spu_id",spuInfo.getId());
            spuInfo.setImages(spuImgMapper.selectList(queryWrapper));
            queryWrapper.clear();
        });
        return spuInfos;
    }

    public Long countCat(Integer parentid){
        return spuInfoMapper.CountByCatParentId(parentid);
    }

    public Long countKey(String keyword){
        return spuInfoMapper.CountByKeyWord(keyword);
    }
}
